Component({
  properties: {
    calendarData: {
      type: Object,
      value: {}
    },
    refundId: {
      type: String,
      value: ""
    },
    bdKey: {
      type: String,
      value: ""
    },
    orderId: {
      type: String,
      value: ""
    },
    isLogin: {
      type: Boolean,
      value: false
    },
    orderStatus: {
      type: Number,
      value: 0
    },
    refundTotalAmount: {
      type: Number,
      value: 0
    },
    mode: {
      type: Number,
      value: 2
    },
    goodsId: {
      type: String,
      value: ""
    },
    goodsType: {
      type: Number,
      value: 1
    },
    goods: {
      type: Object,
      value: {}
    },
    marketingReady: {
      type: Boolean,
      value: false
    }
  },
  data: {},
  methods: {
    getExtPath() {
      return new Promise(resolve => {
        const { goods } = this.data;
        resolve({
          orderConfirm: {
            path: 'pages/limited/index',
            query: {
              itemId: goods.id,
              goodUuid: 'f2a5f284a5664a4d99de7fab47e04593'
            }
          }
        })
      })
    },
    getGoodsInfo() {
      return new Promise(resolve => {
        const { goods, calendarData, bdKey } = this.data;
        if (goods) {
          resolve({
            extra: {
              use_trade_delivery: 0,
              buy_date: "",
              'bd-key': bdKey,
              buy_date_range: calendarData,
              custom_item_detail: {
                arr: [
                  {
                    address: "",
                    comments: "",
                    city: "",
                    num: 1,
                    upload_file: "",
                    face: "",
                    province: "",
                    phone: "",
                    idcard: "",
                    name: "",
                    checked: true,
                    region: "",
                    isInfo: false,
                    mark: ""
                  }
                ],
                custom_item_config: {
                  name: true,
                  phone: true,
                  mark: false,
                  idcard: false,
                  upload_file: false,
                  comments: false,
                  address: false,
                  face: false
                }
              }
            },
            marketingVersion: 2,
            currentPrice: +(goods.price * 100).toFixed(2),
            minLimits: 1,
            maxLimits: goods.max_buy,
            dateRule: `${goods.effective_begin_show_calendar || ""}${goods.effective_end_show_calendar ? goods.effective_end_show_calendar + "可用" : ""}`,
            goodsName: goods.name,
            goodsPhoto: goods.img,
            goodsLabels: goods.dyServiceLabels,
            validation: {
              phoneNumber: {
                required: true // 手机号是否必填
              }
            }
          });
        }
      });
    },
    onError(e) {
      console.log("触发onError", e.detail);
    },
    handleRefund(event) {
      console.log("触发handleRefund", event);
    },
    onPay(options) {
      console.log("触发onPay", options);
      const { status, outOrderNo, result } = options.detail;
      if (status === "success") {
        const { code } = result;
        const resultList = [
          "支付成功",
          "支付超时",
          "支付失败",
          "支付关闭",
          "支付取消"
        ];
        if (code === 0) {
          // 支付成功
          const pages = getCurrentPages();
          if (pages && pages[pages.length - 1] && pages[pages.length - 1].$page && pages[pages.length - 1].$page.fullPath && pages[pages.length - 1].$page.fullPath.indexOf("/pages/order/index") > -1) {
            return;
          }
          tt.redirectTo({url: `usr://main/order/detail?orderId=${outOrderNo}`})
        } else {
          tt.showModal({
            title: resultList[code]
          });
        }
      } else if (status === "fail") {
        if (result.errMsg && result.errMsg.indexOf("fail") > -1) {
          tt.showModal({
            showCancel: false,
            title: result.errMsg.substr(
              result.errMsg.indexOf("fail") + 4,
              result.errMsg.length
            )
          });
        }
        if (result.name && result.name === "USER_RUNTIME_ERROR") {
          if (result.raw && result.raw.errMsg) {
            tt.showModal({
              showCancel: false,
              title: result.raw.errMsg
            });
          } else {
            tt.showModal({
              showCancel: false,
              title: "操作已超时"
            });
          }
        }
      }
    },
    userLogin() {
      return new Promise((resolve, reject) => {
        const {isLogin} = this.data
        if (!isLogin) {
          tt.navigateTo({
            url: "/main/login/login"
          })
          reject()
        } else {
          resolve()
        }
      })
    }
  }
})
